﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using messageboard.model;
using System.Data.SqlClient;
using System.Data;
namespace messageboard.dal
{
    public class UserDal
    {
        private SQLHelper sqlhelper;
        public UserDal() 
        {
            sqlhelper = new SQLHelper();       
        }
        #region 检查用户是否存在
        public Boolean isexist(string name)
        {
            bool flag = false;
            string cmdText = "select * from _user where name=@name ";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@name",name),
            
            };
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                flag = true;
            }
            return flag;

        }
        #endregion


        #region 检查用户名密码
        public Boolean selectname(string name, string pw) 
        
        {
            bool flag = false;
            string cmdText = "select * from _user where name=@name and pw=@pw";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@name",name),
                new SqlParameter("@pw",pw)

            };
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                flag = true;
            }
            return flag;
        
         }
        #endregion


        #region 用户注册
        public bool regist(User user)
        {
            bool flag = false;

            string cmdText = "register";
            SqlParameter[] paras = new SqlParameter[]
           {
               new SqlParameter("@name",user.Name),

               new SqlParameter("@pw",user.Pw),
               new SqlParameter("@email",user.Email),
               new SqlParameter("@age",user.Age),
               new SqlParameter("@sex",user.Sex),
               new SqlParameter("@face",user.Face)
             };
            int re = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.StoredProcedure);
            if (re>0)
            {
                flag = true;    
            }
            return flag;
        }
        #endregion


        #region 通过用户名获取id
        public int getid(string name)
        {
           
            string cmdText = "select id from _user where name=@name";
            SqlParameter[] paras = new SqlParameter[]
           {    
               new SqlParameter("@name",name),
             
             };
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
           int a= Convert.ToInt32(dt.Rows[0][0].ToString());
           return a;
        }
        #endregion


        #region 用户详细信息
        public DataTable getuserdetail(string name)
        {

            string cmdText = "select * from _user where name=@name";
            SqlParameter[] paras = new SqlParameter[]
           {    
               new SqlParameter("@name",name),
             
             };
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
             return dt;
        }
        #endregion


        #region 查看全部用户
        public DataTable getalluser()
        {

            string cmdText = "select * from _user where name not like 'zpl' and name not like '匿名' ";
           
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, CommandType.Text);
            return dt;
        }
        #endregion
    
        #region 删除用户
        public  bool deleteuser(string name)
        {
            bool flag=false;
            string cmdText = "delete _user where name=@name ";
            SqlParameter[] paras = new SqlParameter[]
           {    
               new SqlParameter("@name",name),
             
             };
            int a = sqlhelper.ExecuteNonQuery(cmdText,paras, CommandType.Text);
            if (a>0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
    }
}